﻿function RealSearchCustomer() {
    RequireLookUp = document.getElementById("divRequireLookUp").value;
    ReturnCodeId = document.getElementById("divReturnCodeId").value;
    ReturnNameId = document.getElementById("divReturnNameId").value;

    Name = document.getElementById("divSearchCustomerName").value;
    Address = document.getElementById("divSearchCustomerAddress").value;
    Email = document.getElementById("divSearchCustomerEmail").value;
    Phone = document.getElementById("divSearchCustomerPhone").value;
    Agency = document.getElementById("divCheckAgency").checked;
    Applicant = document.getElementById("divCheckApplicant").checked;
    Client = document.getElementById("divCheckClient").checked;
    ThirdParty = document.getElementById("divCheckThirdParty").checked;
    GE = document.getElementById("divCheckGE").checked;
    Legal = document.getElementById("divCheckLegal").checked;
    IP = document.getElementById("divCheckIP").checked;
    PJ = document.getElementById("divCheckPJ").checked;
    ContactName = document.getElementById("divSearchContactName").value;
    CountryIndex = document.getElementById("divSearchCountryIndex").value;
    ShortName = document.getElementById("divSearchShortName").value;
    CustomerTypeIndex = document.getElementById("divSearchCustomerTypeIndex").value;
    SearchLevel = document.getElementById("divSearchLevel").selectedIndex;
    fromDate = document.getElementById("divSearchCustomerFromDate").value;
    toDate = document.getElementById("divSearchCustomerToDate").value;
    RelationCode = document.getElementById("txtRelationCode").value;
    CurrentPageIndex = 0;
    if (document.getElementById("divSelectPageIndex") != null) {
        CurrentPageIndex = document.getElementById("divSelectPageIndex").selectedIndex;
    }

    RetValue = Portal.Vision.SearchCustomer.ServerSideSearchCustomer(RequireLookUp, ReturnCodeId, ReturnNameId, Name, Address, Phone, Agency.toString(), Applicant.toString(), Client.toString(), ThirdParty.toString(), GE.toString(), Legal.toString(), PJ.toString(), IP.toString(), ContactName, CountryIndex, ShortName, CustomerTypeIndex, SearchLevel, Email, fromDate, toDate, RelationCode, CurrentPageIndex).value;
    if (RetValue.indexOf("ERROR") > -1) {
        alert(RetValue);
        return;
    }
    document.getElementById("divResult").innerHTML = RetValue;
    document.getElementById("divWaiting").innerHTML = "";



}

function SearchCustomerFunc() {
    document.getElementById("divWaiting").innerHTML = Portal.Vision.SearchCustomer.GetProcessingText().value;
    setTimeout("RealSearchCustomer()", 10);
}

function PdfFunc() {
    document.getElementById("divWaiting").innerHTML = "Đang đọc dữ liệu từ máy chủ...";
    setTimeout("RealPdfFunc()", 10);
}

function RealPdfFunc() {
    Name = document.getElementById("divSearchCustomerName").value;
    Address = document.getElementById("divSearchCustomerAddress").value;
    Email = document.getElementById("divSearchCustomerEmail").value;
    Phone = document.getElementById("divSearchCustomerPhone").value;
    Agency = document.getElementById("divCheckAgency").checked.toString();
    Applicant = document.getElementById("divCheckApplicant").checked.toString();
    Client = document.getElementById("divCheckClient").checked.toString();
    ThirdParty = document.getElementById("divCheckThirdParty").checked.toString();
    GE = document.getElementById("divCheckGE").checked.toString();
    Legal = document.getElementById("divCheckLegal").checked.toString();
    IP = document.getElementById("divCheckIP").checked.toString();
    PJ = document.getElementById("divCheckPJ").checked.toString();
    ContactName = document.getElementById("divSearchContactName").value;
    CountryIndex = document.getElementById("divSearchCountryIndex").value;
    ShortName = document.getElementById("divSearchShortName").value;
    CustomerTypeIndex = document.getElementById("divSearchCustomerTypeIndex").value;
    SearchLevel = document.getElementById("divSearchLevel").selectedIndex.toString();
    fromDate = document.getElementById("divSearchCustomerFromDate").value;
    toDate = document.getElementById("divSearchCustomerToDate").value;
    RelationCode = document.getElementById("txtRelationCode").value;
    RetValue = Portal.Vision.SearchCustomer.ServerSidePdfFunc(Name, Address, Phone, Agency, Applicant, Client, ThirdParty, GE, Legal, PJ, IP, ContactName, CountryIndex, ShortName, CustomerTypeIndex, SearchLevel, Email, fromDate, toDate, RelationCode).value;
    document.getElementById("divWaiting").innerHTML = "";
    if (RetValue.indexOf("ERROR") > -1) {
        alert(RetValue);
        return;
    }
    window.open(RetValue, "_blank");
}

function FullPdfFunc() {
    document.getElementById("divWaiting").innerHTML = "Đang đọc dữ liệu từ máy chủ...";
    setTimeout("RealFullPdfFunc()", 10);
}

function RealFullPdfFunc() {
    Name = document.getElementById("divSearchCustomerName").value;
    Address = document.getElementById("divSearchCustomerAddress").value;
    Email = document.getElementById("divSearchCustomerEmail").value;
    Phone = document.getElementById("divSearchCustomerPhone").value;
    Agency = document.getElementById("divCheckAgency").checked.toString();
    Applicant = document.getElementById("divCheckApplicant").checked.toString();
    Client = document.getElementById("divCheckClient").checked.toString();
    ThirdParty = document.getElementById("divCheckThirdParty").checked.toString();
    GE = document.getElementById("divCheckGE").checked.toString();
    Legal = document.getElementById("divCheckLegal").checked.toString();
    IP = document.getElementById("divCheckIP").checked.toString();
    PJ = document.getElementById("divCheckPJ").checked.toString();
    ContactName = document.getElementById("divSearchContactName").value;
    CountryIndex = document.getElementById("divSearchCountryIndex").value;
    ShortName = document.getElementById("divSearchShortName").value;
    CustomerTypeIndex = document.getElementById("divSearchCustomerTypeIndex").value;
    SearchLevel = document.getElementById("divSearchLevel").selectedIndex.toString();
    fromDate = document.getElementById("divSearchCustomerFromDate").value;
    toDate = document.getElementById("divSearchCustomerToDate").value;
    RelationCode = document.getElementById("txtRelationCode").value;

    document.getElementById("divWaiting").innerHTML = "";
    RetValue = Portal.Vision.SearchCustomer.ServerSidePdfFullFunc(Name, Address, Phone, Agency, Applicant, Client, ThirdParty, GE, Legal, PJ, IP, ContactName, CountryIndex, ShortName, CustomerTypeIndex, SearchLevel, Email, fromDate, toDate, RelationCode).value;

    if (RetValue.indexOf("ERROR") > -1) {
        alert(RetValue);
        return;
    }
    window.open(RetValue, "_blank");
}

function ShowContactListFunc(CustIndex) {
    HtmlAdded = Portal.Vision.SearchCustomer.ServerSideShowContactListFunc(CustIndex).value;
    if (HtmlAdded.indexOf("ERROR") > -1) {
        alert(HtmlAdded);
        return;
    }
    document.getElementById("divShowContact_" + CustIndex).innerHTML = HtmlAdded;
}

function ReturnValueFunc(RetCodeId, RetCode, RetCodeName, RetName) {
    window.opener.document.getElementById(RetCodeId).value = RetCode;
    window.opener.document.getElementById(RetCodeName).innerHTML = RetName;
    alert('Chọn thành công! Xin vui lòng quay lại cửa sổ trước!');
    //if(confirm( window.close();
}


function DeleteCustomer(CustIndex) {
    if (confirm("Bạn đã chắc chắn xóa khách hàng này chưa?") == false) return;
    RetValue = Portal.Vision.SearchCustomer.ServerSideDeleteCustomer(CustIndex).value;
    if (RetValue.indexOf("ERROR") > -1) {
        alert(RetValue);
        return;
    }
    alert("Khách hàng đã được xóa. Nhấn Ok và ấn F5 để cập nhật!");
}


function DeleteContact(Id) {
    if (confirm("Bạn đã chắc chắn xóa người liên lạc này chưa?") == false) return;
    ErrorMsg = Portal.Vision.SearchCustomer.ServerSideDeleteContact(Id).value;
    if (ErrorMsg.indexOf("ERROR") > -1) {
        alert(ErrorMsg);
        return;
    }
    alert("Người liên lạc đã được xóa. Nhấn Ok và ấn F5 để cập nhật!");
}